home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / TexturingAndModeling:AProceduralApproach / KMShaders / KMLuna.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  4.7 KB  |  171 lines

  1. /* modified by wave to KMLuna for name space protection... */
  2. /*
  3.  * luna.sl -- surface shader for the moon
  4.  *
  5.  * DESCRIPTION:
  6.  *    Makes a surface that looks sort of like Earth's moon.  It doesn't really
  7.  *    have craters, so it isn't good for closeups.  But it's pretty good at about
  8.  *    the scale for human naked-eye viewing from earth.
  9.  *
  10.  * AUTHOR:
  11.  *    C language version by F. Kenton Musgrave
  12.  *    Translation to Shading Language by Larry Gritz.
  13.  *
  14.  * REFERENCES:
  15.  *    _Texturing and Modeling: A Procedural Approach_, by David S. Ebert, ed.,
  16.  *    F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley.
  17.  *    Academic Press, 1994.  ISBN 0-12-228760-6.
  18.  *
  19.  * HISTORY:
  20.  *    ??? - original C language version by Ken Musgrave
  21.  *    Apr 94 - translation to Shading Language by L. Gritz
  22.  *
  23.  * this file last updated 18 Apr 1994
  24.  */
  25.  
  26.  
  27. #define snoise(Pt) (2*noise(Pt) - 1)
  28.  
  29.  
  30. #define DNoise(p) (2*(point noise(p)) - point(1,1,1))
  31. #define VLNoise(Pt,scale) (snoise(Pt + scale*DNoise(Pt)))
  32. #define TWOPI (6.28)
  33.  
  34.  
  35. surface
  36. KMLuna (float Ka = .5, Kd = 1;
  37.         float lacunarity = 2;
  38.         float octaves = 8;
  39.         float H = .3;
  40.         color highland_color = .7;
  41.         float maria_basecolor = .7, maria_color = .1;
  42.         float arg22 = 1, arg23 = .3;
  43.         float highland_threshold = -0.2;
  44.         float highland_altitude = 0.001, maria_altitude = 0.0004;
  45.         float peak_rad = .0075, inner_rad = .01, rim_rad = .02, outer_rad = .05;
  46.         float peak_ht = 0.005, rim_ht = 0.003;
  47.         float numrays = 8;  /* arg10 */
  48.         float rayfade = 1;  /* arg11 */
  49.        )
  50. {
  51.   float radial_dist;
  52.   point PP, PQ;
  53.   float l, a, o, i, omega;
  54.   float chaos;
  55.   color Ct;
  56.   float temp1;
  57.   point vv;
  58.   float uu, ht, freq, scale;
  59.   float lighten;
  60.   point NN;
  61.   float pd;  /* pole distance */
  62.   float raydist;
  63.  
  64.   PQ = P;
  65.   PP = transform ("shader", P);
  66.   NN = normalize (N);
  67.   radial_dist = sqrt (xcomp(PP)*xcomp(PP) + ycomp(PP)*ycomp(PP));
  68.   omega = pow (lacunarity, (-.5)-H);
  69.  
  70.   /* bumpy = fBm (PP, omega, lacunarity, octaves); */
  71.   l = 1;  o = 1;  a = 0;
  72.   for (i = 0;  i < octaves;  i += 1) {
  73.       a += o * snoise (l * PP);
  74.       l *= lacunarity;
  75.       o *= omega;
  76.     }
  77.   chaos = a;
  78.  
  79.   Ct = Cs;
  80.  
  81.   /* Insure that the crater is in one of the maria */
  82.   temp1 = radial_dist * arg22;
  83.   if (temp1 < 1)
  84.       chaos -= arg23 * (1 - smoothstep (0, 1, temp1));
  85.  
  86.   if (chaos > highland_threshold) {
  87.       PQ += chaos * highland_altitude * NN;
  88.       Ct += highland_color * chaos;
  89.     }
  90.   else {
  91.       PQ += chaos * maria_altitude * NN;
  92.       Ct *= maria_basecolor + maria_color * chaos;
  93.     }
  94.  
  95.  
  96.   /***********************************************************************/
  97.   /* Add crater */
  98.   /* get normalized vector "v" */
  99.   pd = 1-v;
  100.   vv = point (xcomp(PP)/radial_dist, 0, zcomp(PP)/radial_dist);
  101.   lighten = 0;
  102.   if (pd < peak_rad) {      /* central peak */
  103.       uu = 1 - pd/peak_rad;
  104. /*      lighten = uu*uu; */
  105.       ht = peak_ht * smoothstep (0, 1, uu);
  106.     }
  107.   else if (pd < inner_rad) {       /* crater floor */
  108.       ht = 0;
  109.     }
  110.   else if (pd < rim_rad) {           /* inner rim */
  111.       uu = (pd-inner_rad) / (rim_rad - inner_rad);
  112.       lighten = .75*uu;
  113.       ht = rim_ht * smoothstep (0, 1, uu);
  114.     }
  115.   else if (pd < outer_rad) {        /* outer rim */
  116.       uu = 1 - (pd-rim_rad) / (outer_rad-rim_rad);
  117.       lighten = .75*uu*uu;
  118.       ht = rim_ht * smoothstep (0, 1, uu*uu);
  119.     }
  120.   else ht = 0;
  121.   PQ += ht * NN;
  122.   lighten *= 0.2;
  123.   Ct += color(lighten,lighten,lighten);
  124.  
  125.   /* Add some noise */
  126.   if (uu > 0) {
  127.       if (pd < peak_rad) {     /* if on central peak */
  128.       vv = 5*PP + 3 * vv;
  129.       freq = 1;  scale = 1;  ht = 0;
  130.       for (i = 0;  i < 4;  i += 1) {
  131.           ht += scale * snoise (freq * vv);
  132.           freq *= 2;  scale *= 0.833;
  133.         }
  134. /*      ht = wrinkled (vv, 2, .833, 4); */
  135.       PQ += 0.0025 * uu*ht * NN;
  136.         }
  137.       else {
  138.       vv = 6*PP + 3 * vv;
  139.       freq = 1;  scale = 1;  ht = 0;
  140.       for (i = 0;  i < 4;  i += 1) {
  141.           ht += scale * snoise (freq * vv);
  142.           freq *= 2;  scale *= 0.833;
  143.         }
  144. /*      ht = wrinkled (vv, 2, .833, 4); */
  145.       if (radial_dist > rim_rad)
  146.           uu *= uu;
  147.       PQ += 0.0025 * (0.5*uu + 0.5*ht) * NN;
  148.         }
  149.     }
  150.  
  151.  
  152.   /* Make crater rays (PP, arg10, arg11, arg12, arg15, arg24, arg25, radial_dist);, yielding temp1 */
  153.   lighten = 0;
  154.   if (pd >= rim_rad  &&  pd < 0.4) {
  155.       lighten = smoothstep (.15, .5, snoise(62*u));
  156.       raydist = 0.2 + 0.2 * snoise (20 * mod(u+0.022,1));
  157.       lighten *= (1 - smoothstep (raydist-.2, raydist, pd));
  158.     }
  159.   lighten = 0.2 * clamp (lighten, 0, 1);
  160.   Ct += color (lighten, lighten, lighten);
  161.  
  162.  
  163.   /* Recalc normal since we changed P a whole bunch. */
  164. /*  N = normalize (calculatenormal (PQ)); */
  165.  
  166.   /* Shade like matte */
  167.   Oi = 1;
  168.   Ci = Ct * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N),I)));
  169. }
  170.  
  171.